import { debugLogger } from '../utils/index.js'
export function initSend(MonitorSdk) {
  MonitorSdk.sendReports = function (data) {  
    const { appId, baseUrl } = MonitorSdk._CONFIG
    try {
      if (!baseUrl) {
        console.error('请设置上传 url 地址')
        return
      }
      //  图片打点
      const img = new window.Image()
      let i_ = {
        id: generateUniqueID(),
        path: getPageURL(),
        appId, // 当前应用唯一标识
        ...this.sendInfo,
        ...data
      }
      debugLogger.call(this, '上报数据集：' + JSON.stringify(i_))
      const feeTarget = MonitorSdk._CONFIG.baseUrl +'/dig.gif'
      img.src = `${feeTarget}?d=${encodeURIComponent(JSON.stringify(i_))}`
      if (this.sendInfo.behavior.clickList.length > 0) this.sendInfo.behavior.clickList.length = 0
    } catch (error) {
      console.log(error);
      if (this.sendInfo.behavior.clickList.length > 0) this.sendInfo.behavior.clickList.length = 0
      throw '请求错误，请检查资源dig.gif是否存在！'
    }

  }
}
function generateUniqueID() {
  return `v2-${Date.now()}-${Math.floor(Math.random() * (9e12 - 1)) + 1e12}`
}
function getPageURL() {
  return window.location.pathname
}


export default { initSend }
